# -*- coding:utf-8 -*-

import requests
import json
from lxml import etree
import csv
import os
import time


requests.packages.urllib3.disable_warnings()

"""
中国证券投资基金业协会
https://gs.amac.org.cn/amac-infodisc/res/fund/account/index.html
"""


class Content_Get():
    def __init__(self):
        self.headers = {
            'Accept': 'application/json, text/javascript, */*; q=0.01',
            'Accept-Encoding': 'gzip, deflate, br',
            'Accept-Language': 'zh-CN,zh;q=0.9',
            'Content-Type': 'application/json',
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36',
        }

    # 获取首页内容
    def get_contents(self, cursor):
        postData = {
        }

        url = f"https://gs.amac.org.cn/amac-infodisc/api/fund/account?page={cursor}&size=100"
        postData = str(postData)

        res = requests.post(url, headers=self.headers, data=postData, verify=False)
        data_json = json.loads(res.text)
        for d in data_json['content']:
            id = d['id']
            url_child = f'https://gs.amac.org.cn/amac-infodisc/res/fund/account/{id}.html'
            time.sleep(1)
            self.get_detail(url_child)

    # 获取详细内容
    def get_detail(self, url):
        res = requests.get(url, headers=self.headers, verify=False)
        res.encoding = "utf-8"
        root = etree.HTML(res.text)
        dict_data = dict()
        dict_data['产品名称'] = root.xpath('//table[@class="table"]/tbody/tr[1]/td[2]/text()')[0]
        dict_data['产品编码'] = root.xpath('//table[@class="table"]/tbody/tr[2]/td[2]/text()')[0]
        dict_data['管理人名称'] = root.xpath('//table[@class="table"]/tbody/tr[3]/td[2]/text()')[0]
        dict_data['托管人名称'] = root.xpath('//table[@class="table"]/tbody/tr[4]/td[2]/text()')[0]
        dict_data['备案日期'] = root.xpath('//table[@class="table"]/tbody/tr[5]/td[2]/text()')[0]
        dict_data['成立日期'] = root.xpath('//table[@class="table"]/tbody/tr[6]/td[2]/text()')[0]
        dict_data['到期日'] = root.xpath('//table[@class="table"]/tbody/tr[7]/td[2]/text()')[0]
        dict_data['投资类型'] = root.xpath('//table[@class="table"]/tbody/tr[8]/td[2]/text()')[0]
        dict_data['是否分级'] = root.xpath('//table[@class="table"]/tbody/tr[9]/td[2]/text()')[0]
        dict_data['运作状态'] = root.xpath('//table[@class="table"]/tbody/tr[10]/td[2]/text()')[0]

        self.ditc_to_csv(dict_data)

    # 保存数据
    def ditc_to_csv(self, item):
        savefileName = "证券投资基金协会.csv"
        headList = []
        saveList = []

        for k, v in item.items():
            headList.append(k)
            saveList.append(v)

        if os.path.exists(savefileName) == False:
            with open(savefileName, "a", encoding="utf-8-sig", newline="") as csvfile:
                writer = csv.writer(csvfile)
                writer.writerow(headList)

        with open(savefileName, "a", encoding="utf-8-sig", newline="") as csvfile:
            writer = csv.writer(csvfile)
            writer.writerow(saveList)

        print(str(item))


if __name__ == '__main__':
    content_get = Content_Get()
    for i in range(1, 329):
        print(f"============第【{i}】页=============")
        content_get.get_contents(i)